package com.zy.socket.chat1;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * @Author Gong
 * @Description 一直等待一直下发消息服务端功能
 * @dATE 2021/12/19 15:40
 **/
public class Client {
    public static void main(String[] args) {
        DataOutputStream dataOutputStream =null;
        DataInputStream dataInputStream =null;
        try {
            Socket socket = new Socket("127.0.0.1",1245);
             dataOutputStream = new DataOutputStream(socket.getOutputStream());
             dataInputStream = new DataInputStream(socket.getInputStream());
            int id=0;
            while (true){
                //输出给服务器
                dataOutputStream.writeUTF("我是学习"+id);
                id++;
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //获得服务器信息
                String str = dataInputStream.readUTF();
                System.out.println("收到来自服务器的信息："+str);
                if (id==10){
                    break;
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                dataOutputStream.close();
                dataInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
